package com.exam.commonbiz.util;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.exam.commonbiz.R;
import com.exam.commonbiz.base.BasicApplication;

public class DialogUtils {

    public static void showCustomConfirmDialog(Activity activity,
                                               String title,
                                               String message,
                                               String confirmText,
                                               String cancelText,
                                               CommonCallBack commonCallBack) {

        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_custom_confirm);

        // 设置宽度为屏幕的80%
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = (int) (ScreenUtil.getScreenWidth(BasicApplication.app) * 0.8);
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        TextView tvTitle = dialog.findViewById(R.id.tvTitle);
        TextView tvMessage = dialog.findViewById(R.id.tvMessage);
        Button btnConfirm = dialog.findViewById(R.id.btnConfirm);
        Button btnCancel = dialog.findViewById(R.id.btnCancel);

        if (title != null) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }

        if (message != null) {
            tvMessage.setText(message);
        }

        if (confirmText != null) {
            btnConfirm.setText(confirmText);
        }

        if (cancelText != null) {
            btnCancel.setText(cancelText);
        }

        btnConfirm.setOnClickListener(v -> {
            if (commonCallBack != null) {
                commonCallBack.callback(0, null);
            }
            dialog.dismiss();
        });

        btnCancel.setOnClickListener(v -> {
            if (commonCallBack != null) {
                commonCallBack.callback(1, null);
            }
            dialog.dismiss();
        });

        dialog.setCancelable(false);
        dialog.show();
    }
}
